/**
 * box.baidu.com MP3播放器插件
 * @author Mr.Asong
 * @link http://mrasong.com
 */
var bdbox = {
	id_main: 'bdbox',
	id_show: 'bdbox_show',
	id_text: 'text',
	in_name: 'bdbox_in_name',
	in_artist: 'bdbox_in_artist',
	in_autoplay: 'bdbox_in_autoplay',
	bt_preview: 'bdbox_bt_preview',
	bt_add: 'bdbox_bt_add',
	tips:{
		nameEmpty: '请输入歌曲名称！',
		nameDefault: '歌曲名称',
		nameTit: '输入您要添加的歌曲名称！',
		artistDefault: '歌手名',
		artistTit: '填写歌手名可以使搜索结果更准确！',
		autoplayT: '自动播放',
		autoplayF: '手动播放'
	},
	_gid: function(id){ return document.getElementById(id); },
	_val: function(id){ return document.getElementById(id).value ; },
	_html: function(){
		var name = encodeURI( this._val(this.in_name) );
		var artist = ( this._val(this.in_artist)==this.tips.artistDefault ) ? "" : encodeURI( this._val(this.in_artist) );
		var autoplay = this._val(this.in_autoplay)
		var str = '<embed src="http://box.baidu.com/widget/flash/song.swf?name='+ name + '&artist='+ artist + '&autoPlay='+ autoplay ;
		str += '" type="application/x-shockwave-flash" wmode="opaque" allowscriptaccess="always" width="500" height="74"></embed>';
		return str;
	},
	_check: function(){
		if( this._val(this.in_name)==this.tips.nameDefault ){
			alert( this.tips.nameEmpty );
			return false;
		}
		return true;
	},
	_preview: function(){
		if( !this._check() ){ return false; }
		this._gid( this.id_show ).innerHTML = this._html();
	},
	_add: function(){
		if( !this._check() ){ return false; }
		
		var myField = this._gid( this.id_text );
		var myValue = this._html();/*
		if(document.selection){
			myField.focus();   
			sel = document.selection.createRange();   
			sel.value = myValue;   
			sel.select(); 
		}else if (myField.selectionStart || myField.selectionStart == '0') { 
			var startPos = myField.selectionStart; 
			var endPos = myField.selectionEnd; 
			myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length); 
		} else { 
			myField.value += myValue; 
		}*/
		document.getElementById(this.id_text).value += myValue;
		
	},
	init: function(){
		document.writeln('<div id="'+ bdbox.id_main +'">');
		document.writeln('<p>');
		document.writeln('  <input type="text" id="'+ bdbox.in_name +'" title="'+ this.tips.nameTit +'"');
		document.writeln('    onblur="this.value=(this.value==\'\') ? this.defaultValue : this.value"');
		document.writeln('    onfocus="this.value=(this.value==this.defaultValue) ? \'\' : this.value" value="'+ this.tips.nameDefault +'" />');
		document.writeln('  <input type="text" id="'+ bdbox.in_artist +'" title="'+ this.tips.artistTit +'"');
		document.writeln('    onblur="this.value=(this.value==\'\') ? this.defaultValue : this.value"');
		document.writeln('    onfocus="this.value=(this.value==this.defaultValue) ? \'\' : this.value" value="'+ this.tips.artistDefault +'" />');
		document.writeln('  <select id="'+ bdbox.in_autoplay +'">');
		document.writeln('    <option value="true" selected="selected">'+this.tips.autoplayT+'</option><option value="false">'+this.tips.autoplayF+'</option>');
		document.writeln('  </select>');
		document.writeln('  <input type="button" id="'+ bdbox.bt_preview +'" value="Preview" onclick="bdbox._preview()" />');
		document.writeln('  <input type="button" id="'+ bdbox.bt_add +'" value="Add" onclick="bdbox._add()" />');
		document.writeln('</p>');
		document.writeln('<div id="'+ bdbox.id_show +'"></div>');
		document.writeln('</div>');
	}
}
bdbox.init();